home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: peer-news.britain.eu.net!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: C constant expression declarations
- Message-ID: <DMto56.Lo3@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <31229735.41C67EA6@isi.com>
- Date: Thu, 15 Feb 1996 14:55:54 GMT
-
- J.R. Stoner (jstoner@isi.com) wrote:
- : This is a query (I have checked the FAQ) into something that strikes me
- : as a dumbfoundedly stupid issue, but I will ask it anyway :)
-
- : Normally, I will do things such as:
-
- : #define EXPR1 1
- : #define EXPR2 2
-
- : ...and so on. Lately, I have been observing in code from other people
- : equivalent declarations such as:
-
- : #define EXPR1 (1)
- : #define EXPR2 (2)
-
- Whilst, as I'm sure you realise, it's necessary to put brackets round
- things like (a + b), it is not necessary around unsigned constants.
- However, a gotcha is using signed constants, for example
-
- #define EXPR -1
- if(a EXPR) ...
-
- this will complile as 'a - 1', rather than give a syntax error
-
- leaving out the brackets, will not affect correct code, but can cause
- incorrect code to be compiled to something undesired, rather than give
- an error. Having brackets o unsigned constants, just reinforces the habit.
-
- Even wih the brackets, I could still have silent error, for instance
-
- #define EXPR (-1)
- int a(int);
- if(a EXPR) ...
-
- will be compiled as 'a(-1)'. However this compounds two errors, using
- a function name instead of a variable (I assume you meant to compare
- a variable with a value), and omit the comparison operator.
-
- nathan
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
-